package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 通知信息DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "通知详细信息")
public class NoticeDTO {

    @NotNull(message = "通知ID不能为空")
    @Positive(message = "通知ID必须是正整数")
    @Schema(description = "通知唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long noticeId;

    @NotBlank(message = "通知标题不能为空")
    @Schema(description = "通知标题", example = "关于春季学期开学的通知", requiredMode = Schema.RequiredMode.REQUIRED)
    private String title;

    @NotNull(message = "通知类型ID不能为空")
    @Positive(message = "通知类型ID必须是正整数")
    @Schema(description = "通知类型ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long typeId;

    @Schema(description = "通知类型名称", example = "系统通知")
    private String typeName;

    @Schema(description = "通知简要描述", example = "新学期开学安排和注意事项")
    private String description;

    @NotBlank(message = "通知内容不能为空")
    @Schema(description = "通知详细内容", example = "<p>新学期将于3月1日正式开学...</p>")
    private String content;

    @Positive(message = "关联课程ID必须是正整数")
    @Schema(description = "关联的课程ID", example = "5")
    private Long scheduleId;

    @Schema(description = "发送人姓名", example = "张老师")
    private String senderName;

    @NotNull(message = "创建时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "通知创建时间", example = "2024-02-28 10:00:00", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime createTime;

    // 接收方相关字段
    @Schema(description = "是否已读（接收方）", example = "false")
    private Boolean readStatus;

    @PositiveOrZero(message = "已读数量不能为负数")
    @Schema(description = "已读数量（发布方）", example = "150")
    private Long readCount;

    @PositiveOrZero(message = "总接收者数量不能为负数")
    @Schema(description = "总接收者数量（发布方）", example = "200")
    private Long totalRecipients;

    @Schema(description = "接收对象信息", example = "一年级一班、二年级二班")
    private String recipientInfo;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "阅读时间")
    private LocalDateTime readTime;

    /**
     * 获取阅读状态文本
     */
    @Schema(description = "阅读状态文本", accessMode = Schema.AccessMode.READ_ONLY)
    public String getReadStatusText() {
        if (readStatus == null) return "未知";
        return readStatus ? "已读" : "未读";
    }

    /**
     * 计算未读数量
     */
    @Schema(description = "未读数量", accessMode = Schema.AccessMode.READ_ONLY)
    public Long getUnreadCount() {
        if (readCount == null || totalRecipients == null) {
            return 0L;
        }
        return Math.max(0, totalRecipients - readCount);
    }

    /**
     * 计算阅读率
     */
    @Schema(description = "阅读率百分比", accessMode = Schema.AccessMode.READ_ONLY)
    public Double getReadRate() {
        if (readCount == null || totalRecipients == null || totalRecipients == 0) {
            return 0.0;
        }
        return (readCount * 100.0) / totalRecipients;
    }

    /**
     * 判断是否为紧急通知（标题包含紧急关键词）
     */
    @Schema(description = "是否为紧急通知", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isUrgent() {
        return title != null && (
                title.contains("紧急") ||
                        title.contains("重要") ||
                        title.contains("立即") ||
                        title.contains("!!!")
        );
    }

    /**
     * 获取内容摘要（前100字符）
     */
    @Schema(description = "内容摘要", accessMode = Schema.AccessMode.READ_ONLY)
    public String getContentSummary() {
        if (content == null) return "";
        // 去除HTML标签
        String plainText = content.replaceAll("<[^>]*>", "");
        return plainText.length() > 100 ? plainText.substring(0, 100) + "..." : plainText;
    }

    /**
     * 验证通知是否过期（创建时间超过30天）
     */
    @Schema(description = "是否已过期", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isExpired() {
        return createTime != null &&
                createTime.isBefore(LocalDateTime.now().minusDays(30));
    }
}
